




#include <cmath>

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "barchartpoints.h"
#endif

#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "wx/label.h"
#include "wx/barchartpoints.h"
#include "wx/chartsizes.h"



wxBarChartPoints::wxBarChartPoints(
    wxString name,
	ChartColor c,
    bool showlabel
) : wxChartPoints(wxChartPointsTypes::Bar()),
	m_Name(name),
	m_Color(c),
	m_Zoom(1),
	m_BarTag(NONE),
    m_ShowLabel( showlabel) {
}


//----------------------------------------------------------------------E-+++
ChartValue wxBarChartPoints::GetXVal(
	int n
) const {
	return ( m_Points.GetXVal(n) );
}


//----------------------------------------------------------------------E-+++
ChartValue wxBarChartPoints::GetYVal(
	int n
) const {
	return ( m_Points.GetYVal(n) );
}


//----------------------------------------------------------------------E-+++
wxString wxBarChartPoints::GetName(
	int n
) const {
	return ( m_Points.GetName(n) );
}


//----------------------------------------------------------------------E-+++
int wxBarChartPoints::GetCount() const {
	return ( m_Points.GetCount() );
}


//----------------------------------------------------------------------E-+++
ChartColor wxBarChartPoints::GetColor(
	int WXUNUSED(n)
) const {
	return ( 0 );
}


//----------------------------------------------------------------------E-+++
ChartValue wxBarChartPoints::GetMaxX() const {
	return ( m_Points.GetMaxX() );
}


//----------------------------------------------------------------------E-+++
ChartValue wxBarChartPoints::GetMaxY() const {
	return ( m_Points.GetMaxY() );
}


//----------------------------------------------------------------------E-+++
ChartValue wxBarChartPoints::GetMinX() const {
	return ( m_Points.GetMinX() );
}


//----------------------------------------------------------------------E-+++
ChartValue wxBarChartPoints::GetMinY() const {
	return ( m_Points.GetMinY() );
}


void wxBarChartPoints::SetZoom(
	double z
) {
	m_Zoom = z;
}


//----------------------------------------------------------------------E-+++
double wxBarChartPoints::GetZoom() {
    return ( m_Sizes->GetXZoom() );
}


void wxBarChartPoints::SetSizes(
	wxChartSizes *sizes
) {
	m_Sizes = sizes;
}


//----------------------------------------------------------------------E-+++
wxChartSizes* wxBarChartPoints::GetSizes() const {
	return ( m_Sizes );
}


//----------------------------------------------------------------------E-+++
ChartColor wxBarChartPoints::GetColor() const {
	return ( m_Color );
}


void wxBarChartPoints::SetColor(
	ChartColor c
) {
	m_Color =  c;
}


//----------------------------------------------------------------------E-+++
wxString wxBarChartPoints::GetName() const {
	return ( m_Name );
}


void wxBarChartPoints::SetName(
    wxString name
) {
	m_Name =  name;
}



void wxBarChartPoints::Add(
    wxString name,
	ChartValue x,
	ChartValue y
) {
	m_Points.Add( name, x, y );
}



void wxBarChartPoints::Add(
    wxString name,
	ChartValue x,
	ChartValue y,
	ChartColor WXUNUSED(c)
) {



	Add( name, x, y );
}


void wxBarChartPoints::SetDisplayTag(
	wxDISPLAY_LABEL d
) {
	m_BarTag = d;
}


//----------------------------------------------------------------------E-+++
wxDISPLAY_LABEL wxBarChartPoints::GetDisplayTag() const {
	return ( m_BarTag );
}



void wxBarChartPoints::Draw(
    CHART_HPAINT hp,
    CHART_HRECT hr
) {
   
   
   
    double iNodes = ceil( static_cast<double>(GetCount()) );

   
   
   
    wxChartSizes *sizes = GetSizes();

   
   
   
    double ValMax = ceil( sizes->GetMaxY() );
    if ( ValMax == 0 )
        ValMax = 1;

    hp->SetBrush( wxBrush(GetColor(), wxSOLID) );
    hp->SetPen( *wxTRANSPARENT_PEN );

    double x, y;
    for ( int iNode = 0; iNode < iNodes; ++ iNode )
    {
       
       
       
        double xVal  = ceil( GetXVal(iNode) );
        x = hr->x + GetZoom() * xVal * ( sizes->GetWidthBar() *
                sizes->GetNumBar() +
                sizes->GetWidthBar3d() * sizes->GetNumBar3d() +
                sizes->GetGap() );

       
       
       
        y = hr->y + ( (hr->h - sizes->GetSizeHeight())*
                GetYVal(iNode) ) / ValMax ;

        hp->DrawRectangle( static_cast<int>(ceil(x)),
                           static_cast<int>(ceil(hr->h - y)),
                           static_cast<int>(sizes->GetWidthBar() * GetZoom()),
                           static_cast<int>(ceil(y)) );

       
       
       
        if (!m_ShowLabel)
            continue;

        wxString lbl;
        wxLabel wxLbl;
        switch ( GetDisplayTag() )
        {
            case XVALUE:
                lbl.Printf( wxT("%d"), static_cast<int>(xVal));
                wxLbl.Draw( hp, static_cast<int>(ceil(x)),
                            static_cast<int>(ceil(hr->h - y)),
                            GetColor(),
                            lbl,
                            UP);
                break;
            case YVALUE:
                lbl.Printf( wxT("%d"), static_cast<int>(GetYVal(iNode)));
                wxLbl.Draw( hp, static_cast<int>(ceil(x)),
                            static_cast<int>(ceil(hr->h - y)),
                            GetColor(),
                            lbl,
                            UP );
                break;
            case XVALUE_FLOAT:
                lbl.Printf( wxT("%4.1f"), xVal);
                wxLbl.Draw( hp, static_cast<int>(ceil(x)),
                            static_cast<int>(ceil(hr->h - y)),
                            GetColor(),
                            lbl,
                            UP);
                break;
            case YVALUE_FLOAT:
                lbl.Printf( wxT("%4.1f"), GetYVal(iNode));
                wxLbl.Draw( hp, static_cast<int>(ceil(x)),
                            static_cast<int>(ceil(hr->h - y)),
                            GetColor(),
                            lbl,
                            UP );
                break;
            case NAME:
                lbl = GetName(iNode).c_str();
                wxLbl.Draw( hp, static_cast<int>(ceil(x)),
                            static_cast<int>(ceil(hr->h - y)),
                            GetColor(),
                            lbl,
                            UP );
                break;
            case NONE:
            default:
                break;
        }
    }

    hp->SetPen( *wxBLACK_PEN );
}



wxBarChartPoints* wxBarChartPoints::CreateWxBarChartPoints(
    wxString name,
    ChartColor c,
    bool showlabel
) {
    if ( c == wxCHART_NOCOLOR )
        c = wxChartColors::GetColor();

    return new wxBarChartPoints( name, c, showlabel );
}
